package com.vietnam.power.controller;

import java.text.MessageFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.vietnam.power.constant.PageConstants;
import com.vietnam.power.constant.URLConstant;
import com.vietnam.power.domain.ProductDTO;
import com.vietnam.power.persistence.Product;
import com.vietnam.power.service.ProductService;

@Controller
public class CommonController extends AbstractController {
	@Autowired
	private ProductService productService;
	
	@RequestMapping(value = URLConstant.ERROR_PAGE)
	public ModelAndView handleErrorPage(HttpServletRequest request, HttpServletResponse response) {
		// Analyze the servlet exception
		Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
		Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
		// String servletName = (String)
		// request.getAttribute("javax.servlet.error.servlet_name");
		String request_uri = (String) request.getAttribute("javax.servlet.error.request_uri");
		// String message = (String)
		// request.getAttribute("javax.servlet.error.message");

		if (request_uri == null) {
			request_uri = "Unknown";
		}

		String message = MessageFormat.format("Error code: {0}<br>Request URI: {1}<br>Message: {2}", statusCode, request_uri,
				getExceptionMessage(throwable, statusCode));

		request.setAttribute("message", message);

		return retriveModelAndView(PageConstants.ERROR_PAGE);
	}

	private String getExceptionMessage(Throwable throwable, Integer statusCode) {
		if (throwable != null) {
			return throwable.getMessage();
		}
		HttpStatus httpStatus = HttpStatus.valueOf(statusCode);
		return httpStatus.getReasonPhrase();
	}

	@RequestMapping(value = { "/", URLConstant.INDEX }, method = RequestMethod.GET)
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
		return retriveModelAndView(PageConstants.INDEX);
	}

	@RequestMapping(value = URLConstant.DISCOVERY, method = RequestMethod.GET)
	public ModelAndView discovery(HttpServletRequest request, HttpServletResponse response) {
		return retriveModelAndViewWithCategory(PageConstants.DISCOVERY);
	}

	@RequestMapping(value = URLConstant.REGISTER_USER, method = RequestMethod.GET)
	public ModelAndView registerUser(HttpServletRequest request, HttpServletResponse response) {
		return retriveModelAndView(PageConstants.REGISTER_USER);
	}

	@RequestMapping(value = URLConstant.SEARCH_PRODUCT, method = RequestMethod.GET)
	public ModelAndView searchProduct(HttpServletRequest request, HttpServletResponse response) {
		return retriveModelAndViewWithCategory(PageConstants.SEARCH_PRODUCT);
	}

	@RequestMapping(value = URLConstant.PRODUCT_DETAIL, method = RequestMethod.GET)
	public ModelAndView productDetail(HttpServletRequest request, HttpServletResponse response,
			@RequestParam( value = "id") Long id) {
		 ProductDTO productDTO=productService.getProductById(id);
		return retriveModelAndViewWithProduct(PageConstants.PRODUCT_DETAIL,productDTO);
	}
}
